package cm.hyb.Controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import cm.hyb.Controller.model.Cat;

@Controller
@RequestMapping("/Cat")
@SessionAttributes(names= {"cats"})
public class Cats {
	
	private static List<Cat> cats = new ArrayList<>();
	{
		cats.add(new Cat(1,12.6, "大众", 5.7, 136, "black"));
		cats.add(new Cat(2,30.5, "奥迪", 8.6, 225, "red"));
		cats.add(new Cat(3,51.0, "奔驰", 10.5, 321, "black"));
	}
	
	@GetMapping("all")
	public String all(Map<String ,Object> map) {
		map.put("cats", cats);
		return "cs/all";
		
	}
	
	//删除
	@GetMapping("del")
	public String del(@RequestParam("ID") int ID) {
		cats.remove(new Cat(ID, 0.0, null, 0.0, 0, null));
		return "redirect:all";
	}
	
	//添加
	@GetMapping("add")
	public String add() {
		return "cs/addCat";
	}
	@GetMapping("adds")
	public String adds(Double price,String brand,double displacement,int horsepower,String color) {
		int catId = (int) (Math.random()*100);
		cats.add(new Cat(catId, price, brand, displacement, horsepower, color));
		return "redirect:all";
	}
	
	//修改
	@GetMapping("up")
	public String up() {
		return "cs/upCat";
	}
	@GetMapping("ups")
	public String ups(@RequestParam("ID") int ID,Double price,String brand,double displacement,int horsepower,String color) {
		for (Cat cat : cats) {
			if(cat.getID()==ID) {
				cat.setPrice(price);
				cat.setBrand(brand);
				cat.setDisplacement(displacement);
				cat.setHorsepower(horsepower);
				cat.setColor(color);
				return "redirect:all";
			}
		}
		return "redirect:all";
	}

}
